<html>
<!--
Copyright (c) 2015, Apps4Av Inc. (apps4av.com)
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    *     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    *
    *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

authors: zkhan
-->

<head>

    <!-- Latest compiled and minified CSS -->
    <script src="file:///android_asset/jquery.min.js"></script>
    <script src="file:///android_asset/bootstrap.min.js"></script>
    <script src="file:///android_asset/bootstrap-select.min.js"></script>
    <script src="file:///android_asset/bootbox.min.js"></script>
    <script src="file:///android_asset/toastr.min.js"></script>
    <link rel="stylesheet" href="file:///android_asset/bootstrap.min.css">
    <link rel="stylesheet" href="file:///android_asset/bootstrap-select.min.css">
    <link rel="stylesheet" href="file:///android_asset/toastr.min.css">
    <style type="text/css">
        button {color: black; height:40px;}
        input {height:40px; border-radius: 5px;}
        .toast {opacity: 1 !important;}
    </style>

    <meta name="viewport" content="width=device-width, initial-scale=1">

    <script type="text/javascript" >

	toastr.options.timeOut = 2000;

var CELL_TEXT = 0;
var CELL_LAST = 1;


// set list name
function list_setname(name) {
	var list = document.getElementById("list_name");

	list.textContent = "List" + "[" + name + "]";
}


// This will move to a particular row on click
function onclick_row(row) {
	AndroidList.moveTo(row - 1);
}


// Set list line
function set_list_line(row, next, text) {
	// rows here are 1 indexed
	row++;
	var list_table = document.getElementById("list_table");

	if(next == 1) {
		list_table.rows[row].cells[CELL_TEXT].style.backgroundColor = "green";
	}
	else {
		list_table.rows[row].cells[CELL_TEXT].style.backgroundColor = "black";
	}

	list_table.rows[row].cells[CELL_TEXT].textContent = text;
	// set onclick on row
	list_table.rows[row].onclick = (function() {
      var currentI = row;
      return function() {
          onclick_row(currentI);
      }
   })();

}

// add a item to the list
function list_add(text) {
	var list_table = document.getElementById("list_table");

	var row = list_table.insertRow(-1);

	var cell = [];

	// Insert new cells (<td> elements) at the 1st and 2nd position of the "new" <tr> element:
	cell[CELL_TEXT] = row.insertCell(CELL_TEXT);

	cell[CELL_TEXT].textContent = text;
}

// clear the entire list
function list_clear() {
	// clear the table
	var list_table = document.getElementById("list_table");
	for(var row = list_table.rows.length - 1; row >= 1; row--) {
			list_table.deleteRow(row);
	}
}

// delete current item
function list_delete_item() {

	bootbox.confirm("Delete Selected?", function(result) {
		if(result == false) {
			return;
		}
        AndroidList.deleteItem();
	});
}

// delete the list
function save_delete(row) {

	bootbox.confirm("Delete Saved?", function(result) {
		if(result == false) {
			return;
		}
    	var list_save = document.getElementById("save_table");
	    AndroidList.saveDelete(list_save.rows[row].cells[CELL_SAVE_NAME].textContent);
	    toastr.success('List deleted');
	});

}

// remove the entire list
function list_discard() {

	bootbox.confirm("Clear List?", function(result) {
		if(result == false) {
			return;
		}
		AndroidList.discardList();
	});

}

// import the entire list from file
function list_import() {
	// call Android
	var path = document.getElementById("import_input");
	if(path.value == "") {
		toastr.error('Please specify a valid path');
		return;
	}

	bootbox.confirm("Delete List then Import?", function(result) {
		if(result == false) {
			return;
		}
		AndroidList.importFromFile(path.value);
		document.getElementById("list_name").scrollIntoView();
	    toastr.info('Importing list from ' + path.value);
		path.value = "";
		$('[href="#list"]').tab('show');
	});
}

// Add a line to list
function add_list_item() {
	var item = document.getElementById("item_input");
	AndroidList.addToList(item.value);
	// clear when added
	item.value = "";
}

// move up current item
function list_moveup_item() {
	AndroidList.moveUpItem();
}

// move down current item
function list_movedown_item() {
	AndroidList.moveDownItem();
}


var CELL_SAVE_NAME = 0;
var CELL_SAVE_LOAD = 1;
var CELL_SAVE_DELETE = 2;
var CELL_SAVE_LAST = 3;

// load the list
function save_load(row) {
	var list_save = document.getElementById("save_table");
	AndroidList.loadList(list_save.rows[row].cells[CELL_SAVE_NAME].textContent);
	document.getElementById("list_name").scrollIntoView();
	$('[href="#list"]').tab('show');
}

// add the current list to saved list
function save_list_add() {
	var name = document.getElementById("save_name").value;

	if(name == "") {
		toastr.error('Please specify a valid name');
		return;
	}
	AndroidList.saveList(name);
	toastr.success('List saved to ' + name);
}

// clear the entire save list
function save_clear() {
	// clear the table
	var list_save = document.getElementById("save_table");
	for(var row = list_save.rows.length - 1; row >= 1; row--) {
			list_save.deleteRow(row);
	}
}


// Sets buttons on each row
function set_buttons_save() {
	// Set buttons for each entry in list
	var list_save = document.getElementById("save_table");
	for(var i = 1; i < list_save.rows.length; i++) {
		list_save.rows[i].cells[CELL_SAVE_LOAD].innerHTML =
			"<button class='btn btn-block' onclick='save_load(" + i.toString() + ")'>LD</button>";
		list_save.rows[i].cells[CELL_SAVE_DELETE].innerHTML =
			"<button class='btn btn-block' onclick='save_delete(" + i.toString() + ")'>Del</button>";
	}

		// clear save input
	document.getElementById("save_name").value = "";
}

// add an item to the list
function save_add(name) {
	var list_save = document.getElementById("save_table");

	var row = list_save.insertRow(-1);

	var cell = [];

	// Insert new cells (<td> elements) at the 1st and 2nd position of the "new" <tr> element:
	cell[CELL_SAVE_NAME] = row.insertCell(CELL_SAVE_NAME);
	cell[CELL_SAVE_LOAD] = row.insertCell(CELL_SAVE_LOAD);
	cell[CELL_SAVE_DELETE] = row.insertCell(CELL_SAVE_DELETE);

	cell[CELL_SAVE_NAME].textContent = name;

	set_buttons_save();

}

$(document).ready(function(){
    $(".nav-tabs a").click(function(){
        $(this).tab('show');
    });
});


</script>

</head>

<body>


    <ul class="nav nav-tabs" style="display:inline-flex;">
      <li class="active"><a href="#list">List</a></li>
      <li><a href="#load">Load</a></li>
      <li><a href="#save">Save</a></li>
      <li><a href="#import">Import</a></li>
      <li><a href="#help">Help</a></li>
    </ul>

    <div class="tab-content">
      <div id="list" class="tab-pane fade in active">
          <table class="table">
              <tr>
                  <td><h3 ><span id="list_name" class="label label-success">List[]</span></h3></td>
              </tr>
          </table>
          <table class="table" id="list_table" ><th>List Item</th></table>
          <table class="table">
              <tr>
                  <td><button class="btn btn-block" onclick="list_discard()">Clr</button></td>
                  <td><button class="btn btn-block" onclick="list_delete_item()">Del</button></td>
                  <td><button class="btn btn-block" onclick="list_moveup_item()">Up</button></td>
                  <td><button class="btn btn-block" onclick="list_movedown_item()">Dn</button></td>
              </tr>
          </table>
          <table class="table"><th>Add Item To List</th>
              <tr>
                  <td><input id="item_input" spellcheck="false" placeholder="Mixture Full Rich."></td>
                  <td><button class="btn btn-block" onclick="add_list_item()">Add</button></td>
              </tr>
          </table>
      </div>


      <div id="load" class="tab-pane fade">
          <table class="table" id="save_table" ><th>Load a List</th></table>
      </div>


      <div id="save" class="tab-pane fade">
          <table class="table"><th>Save Current List</th>
              <tr>
                  <td><input id="save_name" spellcheck="false" placeholder="Current List Name"/></td>
                  <td><button class="btn btn-block" onclick="save_list_add()">Save</button></td>
              </tr>
          </table>
      </div>


      <div id="import" class="tab-pane fade">
          <table class="table"><th>Import a Saved List File</th>
              <tr>
                  <td><input id="import_input" spellcheck="false" placeholder="/sdcard/list.txt"></td>
                  <td><button class="btn btn-block" onclick="list_import()">Import</button></td>
              </tr>
          </table>
      </div>


      <div id="help" class="tab-pane fade">
          <table class="table"><th>Help</th></table>
          <div style="overflow: auto;">
              <u>You may need to disable Predictive Typing in Android Settings!</u><br>The List screen allows you to sequence check lists for your aircraft. A list's size is limited to 100 lines, and 250 characters per line. Any text beyond these limits will be chopped-off.
              <br>
              <b>To Create a List Line by Line</b>
              <ul>
                  <li>Press the "List" tab</li>
                  <li>This is not a recommended method for long lists.</li>
                  <li>Clear the current list by pressing "Clr"</li>
                  <li>Enter a text item in the "Add Item To List" box, then press Add. The entered text will appear in the list at the end.</li>
                  <li>Touch the added item in the list to move the green bar to it.</li>
                  <li>Move the item to the desired sequence by pressing the Up or Dn button several times.</li>
                  <li>If a wrong item is added by mistake, it can be deleted by touching the item to move the green bar to it, then pressing the Del button.</li>
              </ul>

              <b>To Save a List</b>
              <ul>
                  <li>Press the "Save" tab</li>
                  <li>To save a list, enter the name above the "Save" button, and press the "Save" Button.</li>
                  <li>Press the "Load" tab to confirm that the list was saved.</li>
              </ul>

              <b>To Load/Delete a Saved List</b>
              <ul>
                  <li>Press the "Load" tab to show the saved lists.</li>
                  <li>Press the "LD" button to load the list.</li>
                  <li>You can delete a saved list by pressing the "Del" button to the right of its name in the Saved Lists list.</li>
              </ul>

              <b>To Import a List from a File</b>
              <ul>
                  <li>A long list can be entered in a single action by importing it from disk.</li>
                  <li>Get the list prepared on the PC in text format.</li>
                  <li>Put the list on your device in an every-application-accessible folder like /sdcard/Download, using PC-device USB connection.</li>
                  <li>Press the "Import" tab.</li>
                  <li>Enter the name/path of the file in "Import a Saved List File" box, for example /sdcard/Download/mylist.txt.</li>
                  <li>Press the Import button.</li>
                  <li>Be patient as the import can take up to a minute depending on the file size, and your device's capabilities.</li>
                  <li>The imported list is shown after it is imported.</li>
              </ul>

              <b>To Use a List</b>
              <ul>
                  <li>Simply press "Previous" and "Next" buttons to move the green bar to an item under consideration.</li>
                  <li>Touch an item in the list to move the green bar to it. This is useful for skipping steps.</li>
              </ul>
          </div>
      </div>
  </div>


</body>
</html>